{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 使用numpy.meshgrid() 创建三维网格数组\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的数学本质是创建一个三维空间中的规则网格，其中每个点的坐标由三个数 $(x_1, x_2, x_3)$ 组成。具体来说，代码首先定义了三个一维数组 $x_1, x_2, x_3$，其中 \n",
    "\n",
    "$$\n",
    "x_1 = x_2 = x_3 = [-4, -3, -2, -1, 0, 1, 2, 3, 4]\n",
    "$$\n",
    "\n",
    "这些数组表示三维空间中的离散坐标取值范围，每个维度的取值从 $-4$ 到 $4$，步长为 $1$，即 $9$ 个值。\n",
    "\n",
    "随后，使用 `numpy.meshgrid()` 生成三维网格数据，其中 `xxx1, xxx2, xxx3` 是三个三维数组，它们的形状 (shape) 相同，每个数组的大小均为 $(9,9,9)$，即沿着每个维度都有 $9$ 个数据点。\n",
    "\n",
    "- `xxx1` 表示网格中所有点在 $x_1$ 方向上的坐标值，它的值在第一维上是变化的，而在其他维度上是重复的。\n",
    "- `xxx2` 表示网格中所有点在 $x_2$ 方向上的坐标值，它的值在第二维上是变化的，而在其他维度上是重复的。\n",
    "- `xxx3` 表示网格中所有点在 $x_3$ 方向上的坐标值，它的值在第三维上是变化的，而在其他维度上是重复的。\n",
    "\n",
    "从数学角度来看，`numpy.meshgrid()` 生成的坐标网格 $(xxx1, xxx2, xxx3)$ 可以表示一个离散的三维直角坐标系，每个点 $(i, j, k)$ 在网格中的位置由：\n",
    "\n",
    "$$\n",
    "(x_{1,ijk}, x_{2,ijk}, x_{3,ijk})\n",
    "$$\n",
    "\n",
    "其中：\n",
    "\n",
    "$$\n",
    "x_{1,ijk} = x_1[i]\n",
    "$$\n",
    "$$\n",
    "x_{2,ijk} = x_2[j]\n",
    "$$\n",
    "$$\n",
    "x_{3,ijk} = x_3[k]\n",
    "$$\n",
    "\n",
    "这意味着 `xxx1` 数组的第一维（$x_1$ 轴）是不断变化的，而 `xxx2` 数组的第二维（$x_2$ 轴）是不断变化的，`xxx3` 数组的第三维（$x_3$ 轴）是不断变化的。每个维度上都对其他两个维度进行复制，从而形成规则的三维网格。\n",
    "\n",
    "最后，`xxx1.shape` 返回该数组的形状，即 $(9,9,9)$，表示网格在每个维度上都有 $9$ 个点。`xxx1.ndim` 返回该数组的维度，值为 $3$，表明这是一个三维数据结构。数学上，这样的三维网格用于定义一个离散化的空间，其中可以用于计算物理场（如温度、速度场）、可视化三维函数，或者用于数值模拟等应用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "398d0a9c-4884-4750-a4eb-f234428eb21f",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "592d9f86-794b-4b36-89c3-099726910e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3b97692-7fe2-4f0d-badf-48d46849d7d6",
   "metadata": {},
   "source": [
    "## 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba06a502-610c-49bf-9200-02bb7c7736f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成一维数组，范围 [-4, 4]，步长为 1\n",
    "x1 = np.arange(-4, 5, 1)\n",
    "x2 = x1\n",
    "x3 = x1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63975350-6c23-4a8e-981a-a8bd775c0710",
   "metadata": {},
   "source": [
    "## 三维网格数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e87412bc-0d80-43d5-8998-133654a75faf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成网格\n",
    "xxx1, xxx2, xxx3 = np.meshgrid(x1, x2, x3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]],\n",
       "\n",
       "       [[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "        [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "        [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "        [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "        [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "        [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "        [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "        [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "        [ 4,  4,  4,  4,  4,  4,  4,  4,  4]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xxx1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5f23012e-7064-4507-a029-305d2f8629dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9, 9, 9)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xxx1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "76a09b33-7d2e-4df7-93f5-7e93c293973e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xxx1.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b01b73d1-df8b-4cfc-bf01-278676b0d2cd",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
